<?php
/*
    Lightweight and multiplatform PHP Multithreading Engine
    Copyright (C) 2008  Anton Vedeshin (anton@vedeshin.com, http://anton.vedeshin.com)
	
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

class Thread {
	var $db;
	var $proc_id;
	var $last_beat;
	var $last_busy;
	var $timeout;
	var $state; //0-not ready, 1-ready, 2-n/a, 3-closed
	var $notactive_num;
	function Thread($proc_id) {
		$this->db=new ezSQL_mssql(s_login, s_password, s_db_name_threads, s_host);
		$this->proc_id=$proc_id;
		$this->timeout=500;
		$this->last_busy=0;
		$this->notactive_num=0;
		$query="INSERT INTO threads(proc_id, last_beat) VALUES('".$this->proc_id."','".(time()+60)."');";
		$this->db->query($query);
	}
	static function Create($url,$proc_id) {
		$t = new Thread($proc_id);
		
		//### execute thread
		//NB!!!
		//BE CAREFUL WITH LOG PATHS, IF YOU MISS OR MISSPEL THE PATH, IT IS HARDLY POSSIBLE TO DEBUG
		//IF YOU MISSPELL THE PATH YOU CAN FACE THE PROBLEM OF THREADS SIMPLY DO NOT START OR DO NOT LOG WITHOUT ANY NOTIFICATION
		//USE YOUR OWN PATHS FOR PHP, LOGS AND COMMAND LINE COMMANDS AD PARAMETERS FOR YOUR SPECIFIC OS, WINDOWS EXAMPLE IS BELOW
		//start /B will execute background process in windows, > symbol will store the output of current process into log file
		//you can call threads from another server via http request etc.
		pclose(popen("start /B \"$proc_id\" C:\php\php.exe D:\wwwroot\\newimport\elko\import_ignitor_thread.php > D:\globalimport\logs\\".$proc_id.".txt $proc_id","r"));		
		
		//give some time to start the thread
		Sleeper(1000);
		return $t;
	}
	
	//check is Thread active or not
	//check active, busy, last beat etc.
	//you can put here your own business logic how thread should be checked for statused etc.
	function isActive () {
    if($this->state==3){
			return false;
		}elseif ($this->last_busy==1){
			return true;
		}
		$cur_time=time();
		if($cur_time>$this->last_beat){
			$result=$this->db->get_var("SELECT last_beat FROM threads WHERE proc_id=".$this->proc_id);
			$this->state=$this->db->get_var("SELECT state FROM threads WHERE proc_id=".$this->proc_id);
			if($cur_time<$result){
				return true;
			}
		}else{
			return true;
		}
		return true;
	}
	
	//check is Thread is busy or not, in order to give a new task/job
	//it is similat to the previous procedure
	function isBusy() {
		//$this->tell("ping"); - this could be implemented in the future
		$cur_time=time();
		if($cur_time>$this->last_beat or $this->last_busy==0){
			$result=$this->db->get_var("SELECT busy FROM threads WHERE proc_id=".$this->proc_id);
			$this->last_busy=$result;
			if($result==1){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	
	//tells a command to the thread
	function tell($thought, $params = NULL) {
		$param=base64_encode(serialize($params));
		$query="INSERT INTO cmd(proc_id, cmd, param) VALUES('".$this->proc_id."','".$thought."','".$param."');";
		$this->db->query($query);
	}
}

?>